package com.juzhencms.apps.busi.web;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.juzhencms.apps.busi.entity.AdminLoginUser;
import com.juzhencms.apps.busi.service.OrganizationService;

import lombok.extern.slf4j.Slf4j;

import com.juzhencms.apps.base.busi.service.UploadService;
import com.juzhencms.apps.base.busi.service.UploadService.UploadImgParam;
import com.juzhencms.apps.base.busi.service.UploadService.UploadResult;
import com.juzhencms.apps.base.common.config.Config;
import com.juzhencms.apps.base.common.database.DBTable;
import com.juzhencms.apps.base.common.util.MD5Util;
import com.juzhencms.apps.base.common.util.StringUtil;
import com.juzhencms.apps.base.common.util.WebUtil;


@Controller
@RequestMapping("/checkRecordFile")
@Slf4j
public class CheckRecordFileController  extends BaseController{
	@Autowired
	private UploadService uploadService;
	
	@RequestMapping(value="/getCheckRecordFiles",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String getCheckRecordFiles(HttpServletRequest request) {
		String id=request.getParameter("id");
		//获取当前审批记录
		Map thisCheckRecord=this.getCommonService().getJdbcDao().queryRow("select * from "+DBTable.checkRecord+" where id=? and check_status=0 and del_flag=0", new Object[] {id});
		
		if(thisCheckRecord==null) {
			Map result=new HashMap();
			result.put("result", 0);
			result.put("msg", "没有找到需要审批的数据，请刷新后重试");
			return WebUtil.resultString(result,request);
		}
		Map result=new HashMap();
		//获取当前用户信息
		AdminLoginUser loginUser=this.getLoginUser();
		
		//判断当前用户有无上传权限
		int checkRoleId=Integer.parseInt(thisCheckRecord.get("check_role_id").toString());
		if(thisCheckRecord.get("check_status").toString().equals("0") && loginUser.getRoleIdList().contains(checkRoleId)) {
			result.put("allowUpload", 1);
		}
		
		List imgList=this.getCommonService().getJdbcDao().querySql("select * from "+DBTable.checkRecordFile+" where check_record_id=? and type=1 and del_flag=0 order by id asc limit 0,100", new Object[] {id});
		if(imgList!=null) {
			for(int i=0;i<imgList.size();i++) {
				Map img=(Map)imgList.get(i);
				if(img.get("file")!=null) {
					img.put("file", Config.getFileUrl(img.get("file").toString()));
				}
			}
		}	
		result.put("result", 1);
		result.put("files", imgList);
		
		return WebUtil.resultString(result,request);
	
	}
	
	
	@RequestMapping(value="/uploadCheckRecordFile",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String uploadCheckRecordFile(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,HttpServletResponse response){
		
		String checkRecordId=request.getParameter("checkRecordId");
		if(checkRecordId==null || checkRecordId.equals("")) {
			Map result=new HashMap();
			result.put("result", 0);
			result.put("msg", "参数有误");
			return WebUtil.resultString(result,request);
		}
		
		AdminLoginUser loginUser=this.getLoginUser();
		String rolesId=org.apache.commons.lang3.StringUtils.join(loginUser.getRoleIdList().toArray(), ",");
		if(rolesId==null || rolesId.equals("")) {
			rolesId="0";
		}
		
		Map checkRecord=this.getCommonService().getJdbcDao().queryRow("select * from "+DBTable.checkRecord+" where id=? and del_flag=0 and check_status=0 and check_role_id in("+rolesId+")", new Object[] {checkRecordId});
		if(checkRecord==null) {
			Map result=new HashMap();
			result.put("result", 0);
			result.put("msg", "当前不允许操作，请刷新后重试");
			return WebUtil.resultString(result,request);
		}
		
		UploadResult uploadResult=uploadService.uploadImg(file);
		
		//如果上传成功，返回最新的数据到客户端
		if(uploadResult!=null && uploadResult.getResult()==1) {
			
			//保存图片数据
			Map record=new HashMap();
			record.put("check_record_id", checkRecordId);
			record.put("type", 1);
			record.put("file", uploadResult.getSavePath());
			record.put("create_user", this.getLoginUserId());
			record.put("create_time", new Date());
			
			int id=this.getCommonService().getJdbcDao().addObj(DBTable.checkRecordFile, record);
			record.put("id", id);
			record.put("file", uploadResult.getUrlPath());
			Map result=new HashMap();
			result.put("result", result);
			result.put("data", record);
			
			return WebUtil.resultString(result,request);
			
		}else {
			return WebUtil.resultString(uploadResult,request);
		}
		
		
		
		
		
	}
	
	@RequestMapping(value="/deleteCheckRecordFile",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String deleteCheckRecordFile(HttpServletRequest request) {
		
		String checkRecordId=request.getParameter("checkRecordId");
		if(checkRecordId==null || checkRecordId.equals("")) {
			Map result=new HashMap();
			result.put("result", 0);
			result.put("msg", "参数有误");
			return WebUtil.resultString(result,request);
		}
		
		AdminLoginUser loginUser=this.getLoginUser();
		String rolesId=org.apache.commons.lang3.StringUtils.join(loginUser.getRoleIdList().toArray(), ",");
		if(rolesId==null || rolesId.equals("")) {
			rolesId="0";
		}
		
		Map checkRecord=this.getCommonService().getJdbcDao().queryRow("select * from "+DBTable.checkRecord+" where id=? and del_flag=0 and check_status=0 and check_role_id in("+rolesId+")", new Object[] {checkRecordId});
		if(checkRecord==null) {
			Map result=new HashMap();
			result.put("result", 0);
			result.put("msg", "当前不允许操作，请刷新后重试");
			return WebUtil.resultString(result,request);
		}
		
		
		
		String id=request.getParameter("id");
		
		this.getCommonService().getJdbcDao().delete(DBTable.checkRecordFile, "where check_record_id=? and id=?", new Object[] {checkRecordId,id});
		
		
		Map result=new HashMap();
		result.put("result", 1);
		return WebUtil.resultString(result,request);
	
	}

}
